package cn.gudian.cms.Admin

import cn.gudian.cms.News.ArcType

class AdminUserController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    AdminU u = AuthSession.getUser(session)
    if (u.getAdminRole().toString().indexOf("SYSTEM_ADMIN") >= 0) {
      params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
      [adminUserInstanceList: AdminUser.list(params), adminUserInstanceTotal: AdminUser.count()]
    } else {
      [adminUserInstanceList: [AdminUser.findByUserName(u.userName)], adminUserInstanceTotal: AdminUser.count()]
    }
  }

  def create = {
    def adminUserInstance = new AdminUser()
    adminUserInstance.properties = params
    return [adminUserInstance: adminUserInstance]
  }

  def save = {
    params.loginNum = 1
    params.lastLoginTime = new Date()
    params.lastLoginIP = request.getRemoteAddr()
    def adminUserInstance = new AdminUser(params)
    if (!adminUserInstance.hasErrors() && adminUserInstance.save()) {
      flash.message = "adminUser.created"
      flash.args = [adminUserInstance.id]
      flash.defaultMessage = "管理员创建成功"
      redirect(action: "show", id: adminUserInstance.id)
    }
    else {
      render(view: "create", model: [adminUserInstance: adminUserInstance])
    }
  }

  def show = {
    def adminUserInstance = AdminUser.get(params.id)
    if (!adminUserInstance) {
      flash.message = "adminUser.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "没有找到该管理员"
      redirect(action: "list")
    }
    else {
      return [adminUserInstance: adminUserInstance]
    }
  }

  def edit = {
    def adminUserInstance = AdminUser.get(params.id)
    if (!adminUserInstance) {
      flash.message = "adminUser.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "没有找到该管理员"
      redirect(action: "list")
    }
    else {
      return [adminUserInstance: adminUserInstance]
    }
  }

  def update = {
    def adminUserInstance = AdminUser.get(params.id)
    if (adminUserInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (adminUserInstance.version > version) {
          adminUserInstance.errors.rejectValue("version", "adminUser.optimistic.locking.failure", "在您更新前已被他人更新")
          render(view: "edit", model: [adminUserInstance: adminUserInstance])
          return
        }
      }
      def u = AuthSession.getUser(session);
      if (u.getAdminRole().toString().indexOf("SYSTEM_ADMIN") != -1) {
        if (adminUserInstance.id == 1 && adminUserInstance.userName == "admin") {
          adminUserInstance.realName = params.realName
          adminUserInstance.mobile = params.mobile
          adminUserInstance.email = params.email
          adminUserInstance.arcTypeIDs = params.arcTypeIDs
        } else {
          adminUserInstance.properties = params
        }
      } else {
        adminUserInstance.realName = params.realName
        adminUserInstance.mobile = params.mobile
        adminUserInstance.email = params.email
      }

      if (!adminUserInstance.hasErrors() && adminUserInstance.save()) {
        flash.message = "adminUser.updated"
        flash.args = [params.id]
        flash.defaultMessage = "更新信息成功"
        redirect(action: "show", id: adminUserInstance.id)
      }
      else {
        render(view: "edit", model: [adminUserInstance: adminUserInstance])
      }
    }
    else {
      flash.message = "adminUser.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "没有找到该管理员"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def adminUserInstance = AdminUser.get(params.id)
    if (adminUserInstance.id == 1 || adminUserInstance.userName == "admin") {
      flash.message = "不能删除初始管理员"
      return redirect(action: "list")
    }
    if (adminUserInstance) {
      try {
        adminUserInstance.delete()
        flash.message = "adminUser.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "删除成功"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "adminUser.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "无法删除该记录"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "adminUser.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "AdminUser not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        AdminUser.executeUpdate("delete AdminUser where id in(" + qstr + ")")
        flash.message = "以下管理员被成功删除" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "删除失败"
        redirect(action: "list")
      }
    }

  }
  def changepass = {
    def adminUserInstance
    if (params.id) {
      adminUserInstance = AdminUser.get(params.id)
    } else {
      adminUserInstance = AdminUser.findByUserName(AuthSession.getUser(session).getUserName());
    }
    return [adminUserInstance: adminUserInstance]
  }
  def changepassword = {
    def adminUserInstance = AdminUser.get(params.id)
    AdminU u = AuthSession.getUser(session)
    if (u.getAdminRole().toString().indexOf("SYSTEM_ADMIN") >= 0 || u.userName == adminUserInstance.userName) {
      if (adminUserInstance) {
        if (adminUserInstance.password == params.oldpass) {
          if (params.newpass == params.newpass2) {
            adminUserInstance.password = params.newpass
            adminUserInstance.save()
            flash.message = "密码修改成功"
          } else {
            flash.message = "密码两次输入不重复"
          }
        } else {
          flash.message = "密码错误"
        }
      } else {
        flash.message = "用户不存在"
      }
    } else {
      flash.message = "您没有权限"
    }
    redirect(action: "list")
  }
}
 